package core.scheduledtask;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimerTask;

import oracle.sql.TIMESTAMP;

import org.apache.log4j.Logger;

import core.IEC104Server;

import util.Timehelper;


import dbop.DBCPDatabaseAccess;
import dbop.DBManager;

public class getPassengersTask extends TimerTask {

	private static Logger logger = Logger.getLogger(getPassengersTask.class);
	private final long starttime=System.currentTimeMillis();
	private long nexttime;
	private ArrayList<Object[]> datas=new ArrayList<Object[]>();
	public getPassengersTask() {
		// TODO Auto-generated constructor stub
		nexttime= starttime-3600*24*1000;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			if(Integer.parseInt(Timehelper.gethour(Timehelper.getCurTime()))>4){
				getAndSavedata();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void getAndSavedata() throws SQLException{
		String date= Timehelper.getDate(new Date(nexttime));
		logger.info("data time: "+date);
		Connection conn= DBManager.getmysqlConn("DB.MYSQL");
		if (conn!=null){
			Statement stmt = conn.createStatement();
			
			String sql= "select id,workdate,xlid,xlmc,cph,gsmc,xsfx,lzjwd,bc,inside_num,zdid,front_up_num,front_down_num,back_up_num,back_down_num,create_time from t_passengerscollect where workdate ='"+date+"'";
			
			boolean result = stmt.execute(sql);
			if (result) {
				
				ResultSet res = stmt.getResultSet();
				int count=0;
				boolean insidenull=false;
				datas.clear();
				while (res.next()) {
					count++;
					String id            =res.getString(1);
					String workdate      =res.getString(2);
					String xlid          =res.getString(3);
					String xlmc          =res.getString(4);
					String cph           =res.getString(5);
					String gsmc          =res.getString(6);
					String xsfx          =res.getString(7);
					String lzjwd         =res.getString(8);
					String bc            =res.getString(9);
					String inside_num    =res.getString(10);
					String zdid          =res.getString(11);
					String front_up_num  =res.getString(12);
					String front_down_num=res.getString(13);
					String back_up_num   =res.getString(14);
					String back_down_num   =res.getString(15);
					String create_time   =res.getString(16);
					Date d=Timehelper.str2Date(create_time);
					Object[] objects= new Object[]{id,workdate,xlid,xlmc,cph,gsmc,xsfx,lzjwd,bc,inside_num,zdid,front_up_num,front_down_num,back_up_num,back_down_num,new Timestamp(d.getTime())};
					
//					if (inside_num.length() <= 0) {
//						insidenull = true;
//						break;
//					}
					// if (zdid.length()>0){
					datas.add(objects);
					// }
					
				}
				if ((count>0)&&(!insidenull)){
					
					String insql="  insert into REC_passengers(id,workdate,xlid,xlmc,cph,gsmc,xsfx,lzjwd,bc,inside_num,zdid,front_up_num,front_down_num,back_up_num,back_down_num,create_time) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
					DBCPDatabaseAccess db = new DBCPDatabaseAccess();
					int [] r=db.ExecuteBatch(insql,datas);
					//db.PreStatExecuteUpdate(insql, objects);
					db.Close();
					if (r!=null){
						this.nexttime=this.nexttime+3600*24*1000;
					}
				}
				datas.clear();
				res.close();
			}
			stmt.close();
		}
		conn.close();
	}

}
